﻿using Microsoft.Practices.Prism.StoreApps;
using Microsoft.Practices.Prism.StoreApps.Interfaces;
using Newtonsoft.Json;
using Sumc.Client.ModernUI.Helpers;
using Sumc.Client.ModernUI.Services;
using Sumc.Models.Responses;
using System;
using System.Threading.Tasks;
using System.Windows.Input;

namespace Sumc.Client.ModernUI.ViewModels
{
    public class NewsFlyoutViewModel : BaseViewModel, IFlyoutViewModel
    {
        private ICommand goBackCommand;
        private Action closeFlyout;
        private new Action goBack;
        private string title;
        private string date;
        private string content;
        private readonly INewsRepository newsRepository;
        
        public NewsFlyoutViewModel(INavigationService navigationService,
            IAlertMessageService allertMessageService,
            IFlyoutService flyoutService,
            ISearchPaneService searchPaneService,
            INewsRepository newsRepository) :
            base(navigationService, 
            allertMessageService, 
            flyoutService, 
            searchPaneService)
        {
            this.newsRepository = newsRepository;
        }

        public ICommand GoBackCommand
        {
            get
            {
                if (this.goBackCommand == null)
                {
                    this.goBackCommand = new DelegateCommand(() => this.CloseFlyout());
                }

                return this.goBackCommand;
            }
        }

        public Action CloseFlyout
        {
            get
            {
                return closeFlyout;
            }

            set
            {
                SetProperty(ref closeFlyout, value);
            }
        }

        public new Action GoBack
        {
            get
            {
                return goBack;
            }

            set
            {
                SetProperty(ref goBack, value);
            }
        }

        public string Title
        {
            get
            {
                return this.title;
            }

            set
            {
                this.SetProperty(ref this.title, value);
            }
        }

        public string Date
        {
            get
            {
                return this.date;
            }

            set
            {
                this.SetProperty(ref this.date, value);
            }
        }

        public string Content
        {
            get
            {
                return this.content;
            }

            set
            {
                this.SetProperty(ref this.content, value);
            }
        }

        public void Open(object parameter, Action successAction)
        {
            this.LoadData(async () =>
            {
                var newsGlanceInformation = await JsonConvert.DeserializeObjectAsync<NewsGlanceInformation>(parameter as string);
                var news = await this.newsRepository.GetNews(newsGlanceInformation.Id);
                this.Title = news.Title;
                this.Date = news.Date;
                this.Content = news.Content;
            });
        }
    }
}